package com.peaker.advertisingpro.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.animation.TranslateAnimation
import android.widget.TextView
import java.util.*


/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/3/21.
 *  代码人生
 */
class ScroTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextView(context, attrs, defStyleAttr) {
    init {
        /*    val display = windowManager.defaultDisplay
            val width = display.width
            val height = display.height
            */

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)


    }

    fun setRollDown(context: Context) {
        val size = Point()
        (context as Activity).windowManager.defaultDisplay.getSize(size)
        var off: Int = 0
        val mHandler: Handler = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message) {
                if (msg.what == 0) {
                    val anim = TranslateAnimation(0f, 0f, off.toFloat(), off.toFloat() + 60f)
                    anim.fillAfter = true
                    startAnimation(anim)
                    off += 60
                    if (off >= size.y) {
                        off = -100
                    }
                }
            }
        }
        var timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() { // (1) 使用handler发送消息
                val message = Message()
                message.what = 0
                mHandler.sendMessage(message)
            }
        }, 0, 1000)


    }


    fun setRollRight(context: Context) {
        val size = Point()
        (context as Activity).windowManager.defaultDisplay.getSize(size)
        var off: Int = 0
        val mHandler: Handler = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message) {
                if (msg.what == 0) {
                    val anim = TranslateAnimation(off.toFloat(), off + 60f, 0f, 0f)
                    anim.fillAfter = true
                    startAnimation(anim)
                    off += 60
                    if (off >= size.x) {
                        off = -300
                    }
                }
            }
        }
        var timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() { // (1) 使用handler发送消息
                val message = Message()
                message.what = 0
                mHandler.sendMessage(message)
            }
        }, 0, 1000)


    }

}